import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-form.vue"; //弹框form
import CustomDialog from "@/components/stationProductionPage/HntManage/pouringOrder/WBS/index.vue";
import publicForm1 from "@/components/public/el-forms.vue"; //弹框form

import {
    saveyjQmpGyJZL, //保存
    getJZLByFid, //详情
    getBaseDataitemDetailByItemCode, //数据字典
    commityjQmpGyYzlglByBHZ,
    commityjQmpGyYzlglBySYS,
    getyjQmpGySgphbPageLists, //施工配合比
    getMachineListByHNT, //任务接收单
    getBaseDepartmentUser,
    getSGPHBByFid, //施工配合比详情
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "modelType"
    ],
    data() {
        return {
            activeNames: ["0", "1", "2"],
            fileList: [],
            isEdit: true,
            isEdits: true,
            materialRow: {},
            materialShow: false,
            selected: [],

            handNum: false, //闭合申请单
            firstNum: false, //一级审核

            visible: "",
            title: null,
            isLock: false,
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj, //基础信息
            formData: { //闭合 closeFormObj
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fProjectid: "", //项目id
                fProjectname: "", //项目名称
                fHtnumber: "", //合同编号
                fTasknumber: "", //任务单编号  //预新增提供
                fEngineeringname: "", //工程名称
                fConstructionsite: "", //施工部位
                fConstructionsiteid: "", //施工部位id
                fHnttype: "", //混凝土种类
                fQdlevel: "", //强度等级
                fJzmethod: "", //浇筑方式
                fSlump: "", //塌落度
                fYjvolume: "", //预计方量
                fJzdates: this.common.day(0), //计划浇筑日期fReportdates: this.common.day(0) 
                fReportid: "", //创建人id
                fReportname: "", //创建人姓名
                fReportdates: this.common.day(0) //创建日期
            }, //施工配合比原材料信息
            laboratoryData: { //试验室办理
                fId: "", //fid
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fSgphbnumber: "", //施工配合比编号
                fSgphbdates: this.common.day(0), //施工配合比报告日期
                fWatercement: "", //水胶比
                fDesignedslump: "", //设计塌落度
                fSubbhzname: "", //任务单接收拌合站id
                fSubbhzid: "", //任务单接收拌合站名称
                fSubreceiveid: "", //浇筑令信息接收人id 用,隔开 多选
                fSubreceivename: "", //浇筑令信息接收人姓名 用,隔开 多选
                fSubreceivename1: "",

                fTestername: "", //试验室试验员名称
                fTesterid: "", //试验室试验员id
                fTesterdates: this.common.day(0) //试验室办理时间
            },
            mixStationData: { //拌合站办理中
                fId: "", //fid
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fBhysb: "", //拌合用设备名称
                fBhysbid: "", //拌合用设备id
                fNote: "", //备注
                fBhglname: "", //拌合站管理人姓名
                fBhglid: "", //拌合站管理人id
                fBhgldates: "" //拌合站办理时间
            },

            tableData: [],
            tablesgData: [],

            tableObj: [],
            LocationData: [],

            baseFormData: {},
            mixStationObj: formJs.mixStationObj, //拌合站办理信息
            laboratoryObj: formJs.laboratoryObj, //试验室

            btnShow: true,
            currentDetail: null,
            fCompanynames: null,
            fCompanyids: null,
            modelTypes: null

        };
    },
    components: {
        publicForm,
        CustomDialog,
        publicForm1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        modelType: {
            handler(newVal) {
                if (newVal) {
                    this.modelTypes = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {


                    // this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    // fStatus


                    if (this.statusNum != 1) {
                        this.getDetail({
                                fId: this.currentDetail.fId
                            }) // currentRow
                    }
                }
            },

            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {

                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true

        this.getPerson(this.userInfo.companyId, 0) //项目部





        if (this.statusNum == 1) { //新增
            this.getBaseDataitemDetailByItemCode('Hnttype', 1) //混凝土种类(Hnttype)
            this.getBaseDataitemDetailByItemCode('Qdlevel', 2) //设计强度等级(Qdlevel)
            this.getBaseDataitemDetailByItemCode('Jzmethod', 3) //浇筑方式
            this.getBaseDataitemDetailByItemCode('Slump', 4) //浇筑方式
            this.getDetail({
                fId: "",
                fCompanyid: this.userInfo.companyId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.laboratoryObj.map((item) => {
                item.disabled = true;
            });
            this.mixStationObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = false;
            this.formObj[11].disabled = true;
            this.formObj[12].disabled = true;
            this.isEdits = false

            this.formData.fReportid = this.userInfo.userId; //当前登录人id
            this.formData.fReportname = this.userInfo.realName; //当前登录人名称
            this.formData.fCompanyid = this.userInfo.companyId //标段id
            this.formData.fCompanyname = this.userInfo.companyName //标段名称
        } else if (this.statusNum == 2) { //编辑
            this.getBaseDataitemDetailByItemCode('Hnttype', 1) //混凝土种类(Hnttype)
            this.getBaseDataitemDetailByItemCode('Qdlevel', 2) //设计强度等级(Qdlevel)
            this.getBaseDataitemDetailByItemCode('Jzmethod', 3) //浇筑方式
            this.getBaseDataitemDetailByItemCode('Slump', 4) //浇筑方式
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.laboratoryObj.map((item) => {
                item.disabled = true;
            });
            this.mixStationObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[11].disabled = true;
            this.formObj[12].disabled = true;
            this.isEdits = false

        } else if (this.statusNum == 5) { //办理
            this.formObj.map((item) => {
                item.disabled = true;
            });

            if (this.currentDetail.fStatus == "编辑中") {
                this.handNum = false; //试验室
                this.firstNum = false; //拌合站办理信息
                this.laboratoryObj.map((item) => {
                    item.disabled = true;
                });
                this.mixStationObj.map((item) => {
                    item.disabled = true;
                });


            } else if (this.currentDetail.fStatus == '试验室办理中') {
                this.yj_searchForm()
                this.getMachineListByHNT("0")
                this.handNum = true; //试验室
                this.firstNum = false; //拌合站办理信息
                this.laboratoryObj.map((item) => {
                    item.disabled = true;
                });
                this.laboratoryObj[0].disabled = false;
                this.laboratoryObj[4].disabled = false;
                this.laboratoryObj[5].disabled = false;
                this.mixStationObj.map((item) => {
                    item.disabled = true;
                });
                this.formObj.map((item) => {
                    item.disabled = true;
                });
            } else if (this.currentDetail.fStatus == '拌合站办理中') {
                this.getMachineListByHNT("1")
                this.handNum = true; //试验室
                this.firstNum = true; //拌合站办理信息
                this.laboratoryObj.map((item) => {
                    item.disabled = true;
                });

                this.mixStationObj.map((item) => {
                    item.disabled = false;
                });
                this.mixStationObj[0].disabled = false;
                this.mixStationObj[1].disabled = false;
                this.formObj.map((item) => {
                    item.disabled = true;
                });
            }
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false

            // 闭合
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.laboratoryObj.map((item) => {
                item.disabled = true;
            });
            this.mixStationObj.map((item) => {
                item.disabled = true;
            });
            if (this.currentDetail.fStatus == "编辑中") {
                this.handNum = false; //试验室
                this.firstNum = false; //拌合站办理信息

            } else if (this.currentDetail.fStatus == '试验室办理中') {
                this.handNum = true; //试验室
                this.firstNum = false; //拌合站办理信息

            } else if (this.currentDetail.fStatus == '拌合站办理中') {
                this.handNum = true; //试验室
                this.firstNum = true; //拌合站办理信息

            }
            this.isEdits = true

        }
        this.getHeight();
    },
    methods: {
        refreshList() {},
        materialDialog() {
            this.materialShow = false;
        },
        // 施工配合比详情
        sgdetail(obj) {
            getSGPHBByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    // var data = res.data.yjQmpGySgphb

                    var that = this
                    console.log(33, res.data.yjQmpGySgphb.yjQmpGySgphbmaterials);
                    that.tableData = res.data.yjQmpGySgphb.yjQmpGySgphbmaterials
                    this.$forceUpdate()
                        // setInterval(() => {

                    // }, 3000)



                }
            });
        },
        getWBS(data) { //获取材料信息
            // console.log(888, data.fLongname, data.fNodeid);
            this.formData.fConstructionsite = data.fLongname //施工部位
            this.formData.fConstructionsiteid = data.fNodeid //施工部位id

        },
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            if (a == "fConstructionsite") {
                this.materialShow = true
            } else if (b == "fHnttype") { //混凝土种类
                this.formData.fHnttype = data.label
            } else if (b == 'fQdlevel') { //设计强度
                this.formData.fQdlevel = data.label
            } else if (b == 'fJzmethod') { //浇筑方式
                this.formData.fJzmethod = data.label
            } else if (b == 'fSlump') { //塌落度
                this.formData.fSlump = data.label
            } else if (b == "fSgphbnumber") {
                console.log(11, data);
                // this.formData.Sgphbnumber //施工配合比编号预新增提供
                this.laboratoryData.fSgphbnumber = data.label; //施工配合比报告编号 
                this.laboratoryData.fSgphbdates = data.value.fSgphbdates //施工日期

                this.laboratoryData.fWatercement = data.value.fWatercement //水胶比
                this.laboratoryData.fDesignedslump = data.value.fDesignedslump.split(",") //设计塌落度 用~隔开
                this.sgdetail({
                    fId: data.value.fId
                })
            } else if (b == "fSubbhzname") { //任务单接收拌合站
                this.laboratoryData.fSubbhzname = data.label; //施工配合比报告编号 
                this.laboratoryData.fSubbhzid = data.value //施工日期

            } else if (b == "fBhysb") { //任务单接收拌合站
                this.laboratoryData.fBhysb = data.label; //施工配合比报告编号 
                this.laboratoryData.fBhysbid = data.value //施工日期

            }
            // this.mixStationData.fBhysb: "", //拌合用设备名称
            // this.mixStationData.fBhysbid: "", //拌合用设备id
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },


        // 数据字典
        getBaseDataitemDetailByItemCode(status, num) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {

                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                if (num == 1) { //混凝土种类
                    this.formObj[5].options = arr
                } else if (num == 2) { //设计强度等级
                    this.formObj[6].options = arr

                } else if (num == 3) { //设计强度等级
                    this.formObj[7].options = arr

                } else if (num == 4) { //设计强度等级
                    this.formObj[8].options = arr

                }
                // else if (num == 3) { //混凝土施工部位
                //     this.formObj[6].options = arr

                // }
            })
        },




        // 详情
        getDetail(obj) {

            // var that = this
            getJZLByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpGyYzlgl

                    this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
                    // this.tableObj = data.YjQmpGySgphbmaterial
                    // this.formData.fSgunitid = this.userInfo.companyId //标段id
                    // this.formData.fSgunitname = this.userInfo.companyName //标段名称
                    // this.formData.fJlunitid = this.userInfo.jlCompanyId //项目id
                    // this.formData.fJlunitname = this.userInfo.jlCompanyName //项目名称
                    this.formData = res.data.yjQmpGyYzlgl
                    if (obj.fId == '') {
                        this.formData.fReportdates = this.common.day(0)
                        this.formData.fJzdates = this.common.day(0)
                        this.formData.fReportid = this.userInfo.userId; //当前登录人id
                        this.formData.fReportname = this.userInfo.realName; //当前登录人名称
                        this.formData.fCompanyid = this.userInfo.companyId //标段id
                        this.formData.fCompanyname = this.userInfo.companyName //标段名称
                    } else {
                        if (this.currentDetail.fStatus == '编辑中') {
                            this.formData = res.data.yjQmpGyYzlgl
                        } else if (this.currentDetail.fStatus == '试验室办理中') {
                            this.laboratoryData = res.data.yjQmpGyYzlgl
                            this.laboratoryData.userId = this.userInfo.userId; //当前登录人id//当前登录人id
                            this.laboratoryData.userName = this.userInfo.realName; //当前登录人名称//当前登录人姓名
                            this.laboratoryData.fTestername = this.userInfo.realName; //试验室试验员名称
                            this.laboratoryData.fTesterid = this.userInfo.userId; //试验室试验员id
                            this.laboratoryData.fTesterdates = this.common.day(0) //试验室办理时间
                        } else if (this.currentDetail.fStatus == '拌合站办理中') {
                            this.laboratoryData = res.data.yjQmpGyYzlgl
                            this.mixStationData = res.data.yjQmpGyYzlgl
                            this.tableData = res.data.yjQmpGySgphbmaterials
                            this.laboratoryData.fDesignedslump = data.fDesignedslump.split(",")
                            this.laboratoryData['fSubreceivename1'] = ""
                            this.mixStationData.userId = this.userInfo.userId; //当前登录人id
                            this.mixStationData.userName = this.userInfo.realName; //当前登录人姓名
                            this.mixStationData.fBhglname = this.userInfo.realName; //拌合站管理人姓名
                            this.mixStationData.fBhglid = this.userInfo.userId; //拌合站管理人id
                            this.mixStationData.fBhgldates = this.common.day(0) //拌合站办理时间
                            var fSubreceivename1 = []
                            if (data.fSubreceiveid != '') {
                                var id1 = data.fSubreceiveid.split(",")
                                var name1 = data.fSubreceivename.split(",")
                                id1.map((item, index) => {
                                    console.log(item, index);
                                    fSubreceivename1.push({
                                        label: name1[index],
                                        value: item
                                    })
                                })
                            } else {
                                fSubreceivename1 = []
                            }
                            this.selected = [{
                                    name: "fSubreceivename1",
                                    selected: fSubreceivename1
                                },
                            ]
                        } else if (this.currentDetail.fStatus == '生产中') {
                            this.formData = res.data.yjQmpGyYzlgl
                            this.tableData = res.data.yjQmpGySgphbmaterials
                            this.laboratoryData = res.data.yjQmpGyYzlgl
                            this.mixStationData = res.data.yjQmpGyYzlgl
                            this.laboratoryData.fDesignedslump = data.fDesignedslump.split(",")
                            var fSubreceivename2 = []
                            if (data.fSubreceiveid != '') {
                                var id2 = data.fSubreceiveid.split(",")
                                var name2 = data.fSubreceivename.split(",")
                                id2.map((item, index) => {
                                    console.log(item, index);
                                    fSubreceivename2.push({
                                        label: name2[index],
                                        value: item
                                    })
                                })
                            } else {
                                fSubreceivename2 = []
                            }
                            this.selected = [{
                                    name: "fSubreceivename1",
                                    selected: fSubreceivename2
                                },
                            ]
                            console.log('this.selected:',this.selected);
                            this.handNum = true; //试验室
                            this.firstNum = true; //拌合站办理信息
                        }
                    }
                    this.formData = data

                }
            });
        },
        // 浇筑令信息接收人*
        getPerson(id, status) {
            var obj = {
                fCompanyid: id,
            };
            getBaseDepartmentUser(obj)
                .then((res) => {
                    if (res.errcode == 0) {

                        var arr = res.data.childList[0].personList;
                        var personList = [];

                        arr.map((item) => {
                            personList.push({
                                label: item.fRealname,
                                value: item.fUserid,
                            });

                        });
                        if (status == 0) {
                            this.laboratoryObj[5].options = personList;

                        }

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 施工配合比

        yj_searchForm() {
            var obj = {
                fCompanyid: this.userInfo.companyId, //左侧选中组织机构id
                keyType: "", //上方查询条件材料种类
                // taskNumber: data.taskNumber, //上方查询条件进场编号
                page: 1, //当前页码
                pageSize: 500, //每页显示条数
                userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                modelType: 2, //0-待办 1-已办 传2 表示列表页查询
            }
            this.loading = true;
            var that = this;
            getyjQmpGySgphbPageLists(obj)
                .then((res) => {
                    console.log(res);
                    if (res.errcode == 0) {
                        that.loading = false;
                        this.laboratoryObj[0].options = []
                        res.data.pageInfo.list.map(item => {
                            this.laboratoryObj[0].options.push({
                                label: item.fSgphbnumber,
                                value: item
                            })
                        })

                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        getMachineListByHNT(num) {
            var obj = {
                fCompanyid: this.userInfo.companyId, //左侧选中组织机构id

                stationType: "1", // 1-混凝土拌合站 默认传1
                modelType: num // 0-查站 1-查机
            }
            this.loading = true;
            var that = this;
            getMachineListByHNT(obj)
                .then((res) => {
                    console.log(res);
                    if (res.errcode == 0) {
                        that.loading = false;
                        if (num == 0) { //站
                            this.laboratoryObj[4].options = []
                            res.data.stationmachineList.map(item => {
                                this.laboratoryObj[4].options.push({
                                    label: item.F_MachineName,
                                    value: item.F_MachineId
                                })
                            })
                        } else if (num == 1) {
                            this.mixStationObj[0].options = []
                            res.data.stationmachineList.map(item => {
                                this.mixStationObj[0].options.push({
                                    label: item.F_MachineName,
                                    value: item.F_MachineId
                                })
                            })
                        }


                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        yj_saveForm() {


            // yjQmpGyLlphbrawmaterial

            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称

            this.formData.fCompanyid = this.userInfo.companyId //标段id
            this.formData.fCompanyname = this.userInfo.companyName //标段名称
            this.formData.fProjectid = this.userInfo.projectId //项目id
            this.formData.fProjectname = this.userInfo.projectName //项目名称

            // this.formData.yjQmpGySgphbmaterials = this.tableData
            console.log(this.formData);
            if (this.statusNum == 1 || this.currentDetail.fStatus == '编辑中') { //新增
                if (this.$refs.publicForm.submitForm()) {
                    // this.formData.fDesignedslump = this.formData.fDesignedslump.join(",")
                    this.saveForm(this.formData, 0);
                } else {
                    console.log("error");
                }
            }
            if (this.statusNum != 1) {


                if (this.currentDetail.fStatus == '试验室办理中') {
                    //项目部
                    var name1 = [];
                    var id1 = [];
                    this.laboratoryData.fSubreceivename1.map(item => {
                        name1.push(item.label)
                        id1.push(item.value)
                    })
                    this.laboratoryData.fSubreceiveid = id1.join(",")
                    this.laboratoryData.fSubreceivename = name1.join(",")

                    if (this.$refs.publicForm1.submitForm()) {
                        this.laboratoryData.fDesignedslump = this.formData.fDesignedslump.join(",")
                        this.saveForm(this.laboratoryData, 1);
                    } else {
                        console.log("error");
                    }
                } else if (this.currentDetail.fStatus == '拌合站办理中') {
                    if (this.$refs.publicForm2.submitForm()) {
                        this.mixStationData.fDesignedslump = this.formData.fDesignedslump.join(",")

                        // this.formData.fDesignedslump = this.formData.fDesignedslump.join(",")
                        this.saveForm(this.mixStationData, 2);
                    } else {
                        console.log("error");
                    }
                }
            }


        },
        // 保存
        saveForm(obj, status) {
            var url;
            if (status == 0) {
                url = saveyjQmpGyJZL //新增
            } else if (status == 1) {
                url = commityjQmpGyYzlglBySYS //试验室
            } else if (status == 2) {
                url = commityjQmpGyYzlglByBHZ //拌合站
            }
            this.isLock = true
            url(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};